package view;

import controller.Controleur;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.*;
import javax.swing.border.Border;
import model.Model;
import observer.Observer;

/**
 * Classe representant la fenetre de parametre du son
 * @author Lefevre, Thoue
 */
public class ParamFrame extends JDialog implements Observer{
    private JPanel panelSilence;
    private JPanel panelAudioFormat;
    private JTextField audioSampleRate;
    private JTextField audioSampleSizeInBits;
    private JTextField audioChannels;
    private JCheckBox audioSigned;
    private JCheckBox audioBigEndian;
    
    private JLabel seuilSilence;
    private JSlider sliderSilence;
    private PanelVoice panelVoice;
    /**
     * Constructeur
     * @param controleur controleur de l'application
     */
    public ParamFrame(Controleur controleur){
        this.setVisible(false);
        this.setTitle("Connection eAudioChat");
        this.setLayout(new BorderLayout());
        this.setPreferredSize(new Dimension(350,230));
        this.setResizable(false);
        this.setLocationRelativeTo(null);
        this.pack();
        PanelPrincipal panelPrincipal = new PanelPrincipal();
        this.add(panelPrincipal);
        Border border =  BorderFactory.createLineBorder(Color.black);
        panelSilence = new JPanel();
        panelSilence.setOpaque(false);
        panelSilence.setPreferredSize(new Dimension(250, 80));
        panelSilence.setBorder(BorderFactory.createTitledBorder(border,"Seuil de detection de la voix"));
        panelPrincipal.add(panelSilence,BorderLayout.SOUTH);
        sliderSilence = new JSlider(JSlider.HORIZONTAL,0, 100, 5);
        sliderSilence.setOpaque(false);
        sliderSilence.addChangeListener(new ChangeSilenceListenner(controleur));
        seuilSilence = new JLabel("0");
        panelSilence.add(sliderSilence);
        panelSilence.add(seuilSilence);
        panelVoice= new PanelVoice();
        panelSilence.add(panelVoice);
        
        
        panelAudioFormat = new JPanel();
        panelAudioFormat.setOpaque(false);
        panelAudioFormat.setPreferredSize(new Dimension(250, 100));
        panelAudioFormat.setBorder(BorderFactory.createTitledBorder(border,"Parametres du son"));
       
        audioSampleRate = new JTextField("16000");
        audioSampleSizeInBits = new JTextField("16");
        audioChannels= new JTextField("1");
        audioSigned = new JCheckBox("Signed",true);
        audioSigned.setOpaque(false);
        audioBigEndian = new JCheckBox("Big Endian",true);
        audioBigEndian.setOpaque(false);
        
        panelAudioFormat.add(audioSampleRate);
        panelAudioFormat.add(audioSampleSizeInBits);
        panelAudioFormat.add(audioChannels);
        panelAudioFormat.add(audioSigned);
        panelAudioFormat.add(audioBigEndian);
        
        panelPrincipal.add(panelAudioFormat,BorderLayout.NORTH);
                
        validate();
    }
    /**
     * Getter seuil du silence
     * @return le seuil du silence
     */
    public JLabel getSeuilSilence() {  return seuilSilence; }
    /**
     * Setter du seuil de silence
     * @param seuilSilence seuil du silence
     */
    public void setSeuilSilence(JLabel seuilSilence) {  this.seuilSilence = seuilSilence;  }
    /**
     * Getter du parametre de son : bigEndian
     * @return le parametre du son
     */
    public JCheckBox getAudioBigEndian() {    return audioBigEndian;    }
    /**
     * Getter du parametre de son : channel
     * @return le parametre du son
     */
    public JTextField getAudioChannels() {     return audioChannels;    }
    /**
     * Getter du parametre de son : sampleRate
     * @return le parametre du son
     */
    public JTextField getAudioSampleRate() {     return audioSampleRate;    }
    /**
     * Getter du parametre de son : sampleSizeInBits
     * @return le parametre du son
     */
    public JTextField getAudioSampleSizeInBits() {     return audioSampleSizeInBits;    }
    /**
     * Getter du parametre de son : singed
     * @return le parametre du son
     */
    public JCheckBox getAudioSigned() {      return audioSigned;    }
    /**
     * Methode de mise à jour de la vue
     * @param eaudio model a jour
     */
    @Override
    public void update(Model eaudio) {
        panelSilence.removeAll();
        panelSilence.add(sliderSilence);
        seuilSilence = new JLabel(String.valueOf(eaudio.getSeuilSilence()));
        panelSilence.add(seuilSilence);
        panelSilence.add(panelVoice);
        panelVoice.setSeuil(eaudio.getSeuilSilence());
        panelVoice.setActualVoice(eaudio.getActualVoiceLevel());        
        
        panelAudioFormat.removeAll();
        audioSampleRate = new JTextField(String.valueOf(eaudio.getSampleRate()));
        audioSampleSizeInBits = new JTextField(String.valueOf(eaudio.getSampleSizeInBits()));
        audioChannels= new JTextField(String.valueOf(eaudio.getChannels()));
        audioSigned = new JCheckBox("Signed",eaudio.isSigned());
        audioBigEndian = new JCheckBox("Big Endian",eaudio.isBigEndian());
        audioSigned.setOpaque(false);
        audioBigEndian.setOpaque(false);        
        
        panelAudioFormat.add(new JLabel("Sample Rate"));
        panelAudioFormat.add(audioSampleRate);
        panelAudioFormat.add(new JLabel("Sample Size"));
        panelAudioFormat.add(audioSampleSizeInBits);
        panelAudioFormat.add(new JLabel("Sample Channels"));
        panelAudioFormat.add(audioChannels);
        panelAudioFormat.add(audioSigned);
        panelAudioFormat.add(audioBigEndian);
        
        validate();
        repaint();
        panelVoice.validate();
        panelVoice.repaint();
    }
}